package com.mine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineShopAds;
import com.mine.pojo.MineShopArea;
import com.mine.pojo.MineShopAreaCategory;
import com.mine.pojo.MineShopFunction;
import com.mine.pojo.MineShopInfo;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.ShopService;

@Controller
public class ShopController {

	@Autowired
	ShopService shopService;
	
	/**
	 * 创建店铺
	 * @param shop
	 * @return
	 */
	@RequestMapping(value="/shop/create/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result createShop(@PathVariable String token, @RequestBody MineShopInfo shop){
		if (shop==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = shopService.createShop(token,shop);
		return result;
	}
	
	/**
	 * 更新店铺资料
	 * @param shop
	 * @return
	 */
	@RequestMapping(value="/shop/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateShopInfo(@PathVariable String token,@RequestBody MineShopInfo shop){
		if (token==null||shop==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = shopService.updateShopInfo(token, shop);
		
		return result;
	}
	
	/**
	 * 店铺创建者获取店铺信息
	 * @param token
	 * @return
	 */
	@RequestMapping(value="/shop/manager/getInfo/{token}")
	@ResponseBody
	public Result managerGetShopInfo(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = shopService.getShopInfo(token);
		
		return result;
	}
	
	/**
	 * 用户获取获取店铺展示信息
	 * @param token
	 * @return
	 */
	@RequestMapping(value="/shop/getShopInfoData/{shopId}")
	@ResponseBody
	public Result userGetShopInfo(@PathVariable Long shopId){
		if (shopId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = shopService.userGetShopInfo(shopId);
		
		return result;
	}
	
	/**
	 * 添加商铺的轮播图
	 * @param ads 一条广告数据
	 * @return
	 */
	@RequestMapping(value="/shop/ads/addItem/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addShopAdsItem(@PathVariable String token,@RequestBody MineShopAds ads){
		//System.out.println(ads.getUpdateTime());
		if(ads==null||token==null){
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//to_where点击广告位（轮播图）跳转的方向 0跳转web，此时要设置web_url的展示网页 ; 1跳转到商品详情 此时需要填写goods_id
		if (ads.getToWhere()!=null && ads.getToWhere()==0 && ads.getWebUrl().length()==0) {
			return Result.error("请设置webUrl！");
		}
		if (ads.getToWhere()!=null && ads.getToWhere()==1 && ads.getGoodsId()==null) {
			return Result.error("请设置goodsId！");
		}
		//of_type 所属的类型 0默认属于轮播图 1属于单个图片展示
		Result result = shopService.addShopAdsItem(token, ads);
		return result;
	}
	
	/**
	 *  获取店铺所有轮播图
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="/shop/ads/getList/{token}")
	@ResponseBody
	public Result getShopAdsList(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.getShopAdsList(token);
		return result;
	}
	
	/**
	 *  获取店铺所有可用功能
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="/shop/function/getList/{token}")
	@ResponseBody
	public Result getShopFunctionList(@PathVariable String token){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.getShopFunctionList(token);
		return result;
	}
	/**
	 *  更新店铺某项功能
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="/shop/function/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateShopFunction(@PathVariable String token,@RequestBody MineShopFunction shopFunction){
		if (token==null||shopFunction==null||shopFunction.getId()==null||shopFunction.getStatus()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.updateShopFunction(token, shopFunction);
		return result;
	}
	
	/**
	 * 更新某一条轮播图item数据
	 * @param ads
	 * @return
	 */
	@RequestMapping(value="/shop/ads/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateShopAds(@RequestBody MineShopAds ads,@PathVariable String token){
		
		if (token==null||ads==null||ads.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.updateShopAds(token, ads);
		return result;
	}
	
	/**
	 * 删除某一条轮播图item数据
	 * @param ads
	 * @return
	 */
	@RequestMapping(value="/shop/ads/delete/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteShopAds(@PathVariable String token, @RequestBody MineShopAds ads){
		if (token==null||ads==null||ads.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.deleteShopAds(token, ads);
		return result;
	}
	
	/**
	 * 添加商铺首页区域的分类
	 * @param ads
	 * @return
	 */
	@RequestMapping(value="/shop/area/add/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addShopArea(@PathVariable String token, @RequestBody MineShopAreaCategory shopAreaCategory){
		if (token==null||shopAreaCategory==null||shopAreaCategory.getAreaName()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.addShopArea(token, shopAreaCategory);
		return result;
	}
	
	/**
	 * 
	 * 删除商铺首页区域的分类 删掉该区域分类下面的所有商品（不删除商品表中的商品数据）
	 * @param ads
	 * @return
	 */
	@RequestMapping(value="/shop/area/delete/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteShopArea(@PathVariable String token, @RequestBody MineShopAreaCategory shopAreaCategory){
		if (token==null||shopAreaCategory==null||shopAreaCategory.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.deleteShopArea(token, shopAreaCategory);
		return result;
	}
	
	@RequestMapping(value="/shop/area/deleteOneGoods/{token}/{goodsId}/{areaCategoryId}",method=RequestMethod.POST)
	@ResponseBody
	public Result deleteShopAreaOneGoods(@PathVariable String token,@PathVariable String goodsId,@PathVariable String areaCategoryId){
		if (token==null||areaCategoryId==null||goodsId==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.deleteShopAreaOneGoods(token, goodsId,areaCategoryId);
		return result;
	}
	/**
	 * 修改商铺首页区域的分类
	 * @param ads
	 * @return
	 */
	@RequestMapping(value="/shop/area/update/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result updateShopArea(@PathVariable String token, @RequestBody MineShopAreaCategory shopAreaCategory){
		if (token==null||shopAreaCategory==null||shopAreaCategory.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.updateShopArea(token, shopAreaCategory);
		return result;
	}
	/**
	 * 添加分区里面的商品
	 * @param token
	 * @param shopAreaCategory
	 * @return
	 */
	@RequestMapping(value="/shop/area/addGoods/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result addShopAreaGoods(@PathVariable String token, @RequestBody MineShopArea shopArea){
		if (token==null||shopArea==null||shopArea.getAreaCategoryId()==null||shopArea.getGoodsId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		Result result = shopService.addShopAreaGoods(token, shopArea);
		return result;
	}
	
	/**
	 * 根据商铺id获取商铺的首页数据
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="/shop/{shopId}")
	@ResponseBody
	public Result getShopFirstPageInfo(@PathVariable Long shopId){
		Result result = shopService.getShopFirstPageInfo(shopId);
		return result;
	}
	
	
	
	
}



